angular.module('SpecialJudgment')

.controller('SpecialJudgment.HomeCtrl', ['$scope', '$state', 'SpecialJudgmentHomeService', 'User', 'imanDialog', '$ionicLoading', 'imanUsbPort', function($scope, $state, SpecialJudgmentHomeService, User, imanDialog, $ionicLoading, imanUsbPort) {
    var Barcode = ''; // 条码标识
    $scope.addNote = ''; //备注
    $scope.data = {};
    $scope.disableJudge = true;

    function getBarCodeData(code) {
        imanDialog.showLoading();
        SpecialJudgmentHomeService.getPersonOrMaterielData(code).then(function(data) {
            imanDialog.hideLoading();
            if (data != null) {
                $scope.data = data;
                $scope.disableJudge = false;
            } else {
                imanDialog.alert("单品数据为空", "");
            }
        }, function(error) {
            imanDialog.hideLoading();
            $scope.data = {};
        });
    }

    $scope.submitData = function(qcStatus) {
        var confirmStr = "";
        if(qcStatus===2){
           confirmStr = "让步接收";
        }
        else if(qcStatus===3){
           confirmStr = "品质判定";
        }
        else if(qcStatus===4){
           confirmStr = "返工";
        }
        else if(qcStatus===5){
           confirmStr = "报废";
        }
        imanDialog.confirm('是否特裁为'+confirmStr+"？", '进行特裁').then(function (btn) {
            if (btn) {
                imanDialog.showLoading();
                SpecialJudgmentHomeService.submitData(Barcode, qcStatus, $scope.addNote).then(function(data) {
                    if(data){
                       imanDialog.hideLoading();
                       imanDialog.toastBottom("特裁成功");
                       $scope.disableJudge = true; 
                    }
                    else{
                       imanDialog.hideLoading();
                       imanDialog.toastBottom("特裁失败");
                       $scope.disableJudge = false;   
                    }
                    
                }, function(res) {
                    imanDialog.hideLoading();
                    imanDialog.toastBottom("特裁失败");
                    $scope.disableJudge = false; 
                });
            } else {
                
            }
        });
        
    };

    $scope.prompt = function() {
        imanDialog.prompt('请输入文本', '备注').then(function(text) {
            $scope.addNote = text; //备注
        });
    };

    $scope.$on('$ionicView.afterEnter', function() {
        imanUsbPort.start();
    });
    $scope.$on('$ionicView.beforeLeave', function() {
        imanUsbPort.stop();
    });
    $scope.$on('imanUsbPort.input', function(e, inputStr) {
        Barcode = inputStr;
        getBarCodeData(Barcode);
    });
}]);